<template>
  <div class="page">
    <v-pageTitle title="工单列表"></v-pageTitle>
    <v-pageSearch>
      <el-form :inline="true">
         <el-form-item>
           <el-button  type="primary" size="small" @click="toNew">新建工单</el-button>
         </el-form-item>
      </el-form>
    </v-pageSearch>
    
    <v-pageTable pagination :sizeChange="sizeChange" :pageChange="pageChange" :paginationTotal="paginationTotal" paginationAlign="center">
        <el-table v-loading="tableData.loading" :data="tableData.body" border style="width: 100%" :row-style="isActive" :default-sort="{prop: 'date', order: 'descending'}">
            <el-table-column type="index" label="序号" width="80" align="center">
            </el-table-column>
            <el-table-column prop="date" label="日期" width="120" sortable>
						</el-table-column>
            <el-table-column prop="content" label="内容" min-width="180" sortable>
						</el-table-column>
            <el-table-column prop="regestDate" label="登记时间" width="120" sortable>
						</el-table-column>
            <el-table-column prop="auditor" label="审核人" width="100" sortable>
						</el-table-column>
            <el-table-column prop="statue" label="状态" width="120" sortable>
              <template slot-scope="scope">
                <el-tag
                  :type="scope.row.statue | statusFilterType">{{scope.row.statue | statusFilter}}</el-tag>
              </template>
						</el-table-column>
            <el-table-column prop="reason" label="未通过原因" min-width="180" sortable>
						</el-table-column>
            <el-table-column fixed="right" label="操作" min-width="200">
                <template slot-scope="scope">
                  <el-button  @click="handleEdit(scope.$index, scope.row)" :disabled="isEdit(scope.row)" type="text" size="small"><i class="fa fa-edit fa-lg"></i></el-button>
                  <el-button  @click="handleDel(scope.$index, scope.row)" :disabled="isEdit(scope.row)" type="text" size="small"><i class="fa fa-trash-o fa-lg"></i></el-button>
                  <el-button  @click="toDetail(scope.row)" type="text" size="small"><i class="fa fa-bars fa-lg"></i></el-button>
                  <el-button  @click="handleWarn(scope.$index, scope.row)" :disabled="isWarn(scope.row)" type="text" size="small"><i class="fa fa-bell-o fa-lg"></i></el-button>
                  <el-button  type="text" size="small"><i class="fa fa-share-alt fa-lg"></i></el-button>
                  <el-button  type="text" size="small"><i class="fa fa-thumbs-up fa-lg"></i></el-button>
                </template>
            </el-table-column>
        </el-table>
    </v-pageTable>

    <!--编辑界面-->
    <el-dialog title="编辑" v-model="editFormVisible" :close-on-click-modal="false">
      <el-form :model="editForm" label-width="80px" :rules="editFormRules" ref="editForm">
        <el-form-item label="日期" prop="date">
          <el-date-picker type="date" placeholder="选择日期" v-model="editForm.date"></el-date-picker>
        </el-form-item>
        <el-form-item label="内容" prop="content">
          <el-input type="textarea" v-model="editForm.content"></el-input>
        </el-form-item>
        <el-form-item label="登记日期">
          <el-date-picker type="date" placeholder="" disabled v-model="editForm.regestDate"></el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button  @click.native="editFormVisible = false">取消</el-button>
        <el-button  type="primary" @click.native="editSubmit" :loading="editLoading">提交</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
  export default {
    data() {
      return {
        editFormVisible: false,//编辑界面是否显示
        editLoading: false,
				editFormRules: {
          date: [
            { type: 'date', required: true, message: '请选择日期', trigger: 'change' }
          ],
					content: [
						{ required: true, message: '请输入内容', trigger: 'blur' }
					]
				},
        tableData: {
            loading: true,
            body: []
        },
        //编辑界面数据
				editForm: {
          id: 0,
					date: '',
					content: '',
					regestDate: ''
				},
        paginationTotal: 100,
        sels:[]
      }
    },
    filters: {
      statusFilter(statue) {
        const statusMap = ['审核通过','已提醒','未审核','审核不通过']
        return statusMap[statue]
      },
      statusFilterType(statue) {
        const statusMapType = ['primary','success','danger','error']
        return statusMapType[statue]
      }
    },
    methods: {
      getTableData() {
          // ajax 查询表格数据
          this.tableData.body = [
            {
              id: 1,
              date: '2016-01-04',
              content: '今天干了嘛？',
              regestDate: '2017-11-07',
              auditor: 'Jesen',
              statue: 3,
              reason: '与事实不符',
              province: '上海',
              city: '普陀区',
              address: '上海市普陀区金沙江路 1518 弄',
              zip: 200333
            }, {
              id: 2,
              date: '2016-02-01',
              content: 'XXXXXXXXXX',
              regestDate: '2017-11-07',
              auditor: '许静海',
              statue: 2,
              reason: '与事实不符',
              province: '上海',
              city: '普陀区',
              address: '上海市普陀区金沙江路 1518 弄',
              zip: 200333
            }, {
              id: 3,
              date: '2016-11-03',
              content: '没干好啊！',
              regestDate: '2017-11-07',
              auditor: 'ZZZ',
              statue: 1,
              reason: '与事实不符',
              province: '上海',
              city: '普陀区',
              address: '上海市普陀区金沙江路 1518 弄',
              zip: 200333
            }, {
              id: 4,
              date: '2016-12-02',
              content: 'XXXXXXXXXX',
              regestDate: '2017-11-07',
              auditor: 'Jesen',
              statue: 0,
              reason: '与事实不符',
              province: '上海',
              city: '普陀区',
              address: '上海市普陀区金沙江路 1518 弄',
              zip: 200333
            }, {
              id: 5,
              date: '2016-04-04',
              content: 'XXXXXXXXXX',
              regestDate: '2017-11-07',
              auditor: '郑山',
              statue: 1,
              reason: '与事实不符',
              province: '上海',
              city: '普陀区',
              address: '上海市普陀区金沙江路 1518 弄',
              zip: 200333
            }, {
              id: 6,
              date: '2016-06-01',
              content: 'XXXXXXXXXX',
              regestDate: '2017-11-07',
              auditor: '吴先锋',
              statue: 3,
              reason: '与事实不符',
              province: '上海',
              city: '普陀区',
              address: '上海市普陀区金沙江路 1518 弄',
              zip: 200333
            }, {
              id: 7,
              date: '2016-07-03',
              content: 'XXXXXXXXXX',
              regestDate: '2017-11-07',
              auditor: '陈光明',
              statue: 0,
              reason: '与事实不符',
              province: '上海',
              city: '普陀区',
              address: '上海市普陀区金沙江路 1518 弄',
              zip: 200333
            }, {
              id: 8,
              date: '2016-03-02',
              content: 'XXXXXXXXXX',
              regestDate: '2017-11-07',
              auditor: '曾建清',
              statue: 1,
              reason: '与事实不符',
              province: '上海',
              city: '普陀区',
              address: '上海市普陀区金沙江路 1518 弄',
              zip: 200333
            }, {
              id: 9,
              date: '2016-12-04',
              content: 'XXXXXXXXXX',
              regestDate: '2017-11-07',
              auditor: 'Audit',
              statue: 0,
              reason: '与事实不符',
              province: '上海',
              city: '普陀区',
              address: '上海市普陀区金沙江路 1518 弄',
              zip: 200333
            },{
              id: 13,
              date: '2016-05-01',
              content: 'XXXXXXXXXX',
              regestDate: '2017-11-07',
              auditor: '张晓萌',
              statue: 2,
              reason: '与事实不符',
              province: '上海',
              city: '普陀区',
              address: '上海市普陀区金沙江路 1518 弄',
              zip: 200333
            }
          ]
          // 模拟加载
          setTimeout(() => {
              this.tableData.loading = false
          }, 1000)
      },
      // 是否已编辑
      isEdit: function (row) {
        // console.log(row.statue)
        return row.statue == 0 || row.statue == 3 ? true:false;
      },
      // 是否已提醒
      isWarn: function (row) {
        // console.log(row.statue)
        return row.statue == 2 ? false:true;
      },
      //状态显示转换
      isActive: function (row) {
        // return row.statue == 3 ? { "color": "red" } : {};
      },
      //删除
			handleDel: function (index, row) {
				this.$confirm('确认删除该记录吗?', '提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true;
					//NProgress.start();
					let para = { id: row.id };
					removeUser(para).then((res) => {
						this.listLoading = false;
						//NProgress.done();
						this.$message({
							message: '删除成功',
							type: 'success'
						});
						this.getUsers();
					});
				}).catch(() => {

				});
			},
			//跳转到编辑工单界面
			handleEdit: function (index, row) {
        this.$router.push({ path: '/orderInfo/edit', query:{obj: row.id} })
			},
      //显示编辑界面
			handleWarn: function (index, row) {
				this.editFormVisible = true;
				this.editForm = Object.assign({}, row);
			},
      sizeChange(val){
        console.log(val)
        this.getTableData()
      },
      toNew(){
        this.$router.push({ path: '/orderInfo/new?' + new Date() })
      },
      toDetail(row){
        this.$router.push({ path: '/orderInfo/detail', query:{obj: row.id+','+row.statue} })
      },
      pageChange(val){
        console.log(val)
        this.getTableData()
      },
      handleClick(row) {
        console.log(row);
      }
    },
    mounted() {
        // 获取table数据
        this.getTableData()
    }
  }
</script>